import random

import logo.Mlog
from event.event import take_screenshot
from event.mouseController import MouseController
from match.photo import find_icons_semblance_screenshot
from tool.vector import Vector


class YG:
    def __init__(self):
        self.pot: Vector = None
        self.r: float = 0
        self.is_ok: bool = False
        self.mouse_controller: MouseController = MouseController()
        self.direction: Vector = None
        self.get_yg()

    def getpot(self, path: [str]):
        icon_paths = path
        # 截取屏幕截图
        screenshot = take_screenshot()
        # 查找ICO图标在截图中的位置
        matches, annotated_screenshot = find_icons_semblance_screenshot(screenshot, icon_paths)
        return matches

    def get_yg2(self) -> bool:
        # self.pot = Vector(261, 748)
        # self.r = 122
        # return True
        if not self.is_ok:
            pot1 = self.getpot(['./tasks/icos/yg/yg.png'])
            # pot2 = self.getpot(['./tasks/icos/yg/r.png'])
            if len(pot1) == 0:
                return False
            x = int(pot1[0].center_x)
            y = int(pot1[0].center_y)
            self.pot = Vector(x, y)
            r = x / 2
            self.r = r
            logo.Mlog.logger.warning(f"遥感坐标={self.pot},r={r}")
            self.is_ok = True
            return True

    def get_yg(self) -> bool:
        self.pot = Vector(261, 748)
        self.r = 122
        return True
        if not self.is_ok:
            pot1,st = self.getpot(['./icos/yg/yx.png'])
            pot2,st = self.getpot(['./icos/yg/xzjs.png'])
            if len(pot1) == 0 or len(pot2) == 0:
                return False
            # print(6666666666)
            # print(pot1)
            # 9.0852
            x = (pot1[0].center_x - pot2[0].center_x) * 0.85122
            y = (pot1[0].center_y - pot2[0].center_y) * 0.40588
            r = x * 0.10923
            # print(x, y, r)
            # click_icons(int(x), int(y))
            x = int(pot1[0].center_x - x)
            y = int(pot1[0].center_y + y)
            self.pot = Vector(x, y)
            self.r = r
            self.is_ok = True
            return True

    def move(self, self_pot: Vector, target: Vector, duration: float = 0):
        direction = target - self_pot
        # logo.Mlog.logger.warning(f"长度={direction.length},r={self.r}")
        # if direction.length < self.r:
        #     return
        norm = direction.normal()
        if norm == 0:
            return self.pot  # 如果角色已经在敌人位置上，返回摇杆中心位置
        p = norm.scale_new(self.r)
        dj_x = p.x + self.pot.x
        dj_y = p.y + self.pot.y
        self.direction = Vector(dj_x, dj_y)
        # logo.Mlog.logger.warning(f"点击坐标x={dj_x}y={dj_y}")
        self.mouse_controller.set_mouse(dj_x, dj_y, duration)
        # click_icons_pot(dj_x, dj_y)

    def move_reverse(self, duration: float = 0):
        if self.direction is None: return
        v = self.direction.negate()
        logo.Mlog.logger.warning(f"向原来的方向 反向 移动一段x={v.x}y={v.y}")
        self.mouse_controller.set_mouse(v.x, v.y, duration)

    def move_direction(self, duration: float = 0):
        if self.direction is None: return
        logo.Mlog.logger.warning(f"向原来的方向移动一段x={self.direction.x}y={self.direction.y}")
        self.mouse_controller.set_mouse(self.direction.x, self.direction.y, duration)
        # click_icons_pot(dj_x, dj_y)

    def random_move(self, duration: float = 0):
        dj_x = self.r + self.pot.x
        dj_y = self.pot.y
        p = Vector(dj_x, dj_y)
        ng = random.randint(0, 360)
        p.set_angle(ng)
        logo.Mlog.logger.warning(f"随机{ng} 点击坐标x={dj_x}y={dj_y}")
        self.mouse_controller.set_mouse(p.x, p.y, duration)

    def up_move(self, duration: float = 0):
        dj_x = self.pot.x
        dj_y = self.pot.y - self.r
        # logo.Mlog.logger.warning(f"上移动x={dj_x}y={dj_y}")
        self.mouse_controller.set_mouse(dj_x, dj_y, duration)

    def down_move(self, duration: float = 0):
        dj_x = self.pot.x
        dj_y = self.pot.y + self.r
        # logo.Mlog.logger.warning(f"下移动x={dj_x}y={dj_y}")
        self.mouse_controller.set_mouse(dj_x, dj_y, duration)

    def left_move(self, duration: float = 0):
        dj_x = self.pot.x - self.r
        dj_y = self.pot.y
        # logo.Mlog.logger.warning(f"左移动x={dj_x}y={dj_y}")
        self.mouse_controller.set_mouse(dj_x, dj_y, duration)

    def right_move(self, duration: float = 0):
        dj_x = self.pot.x + self.r
        dj_y = self.pot.y
        # logo.Mlog.logger.warning(f"右移动x={dj_x}y={dj_y}")
        self.mouse_controller.set_mouse(dj_x, dj_y, duration)
